﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
using MySql.Data.MySqlClient;
using MySql.Data;

namespace MicroLib
{
    public class Util
    {
        public static int method = 0;
        public static string server;
        public static string sqluser, user;
        public static string sqlpasswd, password, cookie;
        public static string database;
        public static string port;
        public static string startid;
        public static int degree;
        
        public static void GetAvailableAccount()
        {
            if (Util.server == null)
                InitConfig();

            string connStr = "server=" + Util.server + ";user=" + Util.sqluser + ";database=" + Util.database + ";port=" + Util.port + ";password=" + Util.sqlpasswd + ";charset=utf8;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();

            string sql = "call spGetAccount()";
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            MySqlDataReader dr = cmd.ExecuteReader();
            dr.Read();

            user = (string)dr["username"];
            password = (string)dr["passwd"];

            conn.Close();

        }
        public static void InitConfig()
        {
            
            XmlDocument config = new XmlDocument();
            config.Load("Config.xml");

            user = config.SelectSingleNode("Configurations/authentication/user").InnerText;
            password = config.SelectSingleNode("Configurations/authentication/passwd").InnerText;
            cookie = config.SelectSingleNode("Configurations/authentication/cookie").InnerText;
            method = int.Parse(config.SelectSingleNode("Configurations/method").InnerText);

            server = config.SelectSingleNode("Configurations/mysql/host").InnerText;
            sqluser = config.SelectSingleNode("Configurations/mysql/user").InnerText;
            sqlpasswd = config.SelectSingleNode("Configurations/mysql/passwd").InnerText;
            database = config.SelectSingleNode("Configurations/mysql/database").InnerText;
            port = config.SelectSingleNode("Configurations/mysql/port").InnerText;
            
            startid = config.SelectSingleNode("Configurations/startid").InnerText;
            degree = int.Parse(config.SelectSingleNode("Configurations/degree").InnerText);
            
        }

        public static void AddLog(string log)
        {
            Console.WriteLine(log);

            FileStream fs = new FileStream("log.txt",
                FileMode.Append, FileAccess.Write, FileShare.None);

            StreamWriter swFromFileStream = new StreamWriter(fs);
            swFromFileStream.Write(System.DateTime.Now.ToString()+":"+ log + "\n");
            swFromFileStream.Flush();
            swFromFileStream.Close();

            fs.Close();
            fs.Dispose();

        }
    }
}
